package com.example.gfsquadwebviewproject.fragment.setting;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.gfsquadwebviewproject.R;
import com.example.gfsquadwebviewproject.data.setting.SettingConfigData;

public class OtherSettingFragment extends Fragment {

    Context context;

    EditText et_setting_view_timeout;
    EditText et_login_view_timeout;
    EditText et_is_success_refresh_timeout;
    EditText et_is_failure_refresh_timeout;

    Button bt_save_other_setting;


    SettingConfigData settingConfigData;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
        settingConfigData = new SettingConfigData(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.setting_other,container,false);

        et_setting_view_timeout = view.findViewById(R.id.et_setting_view_timeout);
        et_login_view_timeout = view.findViewById(R.id.et_login_view_timeout);
        et_is_success_refresh_timeout = view.findViewById(R.id.et_is_success_refresh_timeout);
        et_is_failure_refresh_timeout = view.findViewById(R.id.et_is_failure_refresh_timeout);

        bt_save_other_setting = view.findViewById(R.id.bt_save_other_setting);

        et_setting_view_timeout.setText(""+settingConfigData.getSetting_view_timeout());
        et_login_view_timeout.setText(""+settingConfigData.getLogin_view_timeout());

        et_is_success_refresh_timeout.setText(""+settingConfigData.getSuccess_refresh_timeout());
        et_is_failure_refresh_timeout.setText(""+settingConfigData.getFailure_refresh_timeout());

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        bt_save_other_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String setting_view_timeout = et_setting_view_timeout.getText().toString();
                String login_view_timeout = et_login_view_timeout.getText().toString();
                String success_refresh_timeout = et_is_success_refresh_timeout.getText().toString();
                String failure_refresh_timeout = et_is_failure_refresh_timeout.getText().toString();


                if (setting_view_timeout.length()==0){
                    et_setting_view_timeout.setError("请输入");
                    return;
                }

                if (login_view_timeout.length()==0){
                    et_login_view_timeout.setError("请输入");
                    return;
                }

                if (success_refresh_timeout.length()==0){
                    et_is_success_refresh_timeout.setError("请输入");
                    return;
                }
                if (failure_refresh_timeout.length()==0){
                    et_is_failure_refresh_timeout.setError("请输入");
                    return;
                }


                if (Integer.valueOf(setting_view_timeout)<3){
                    et_setting_view_timeout.setError("请输入大于3的数");
                    return;
                }
                if (Integer.valueOf(login_view_timeout)<3){
                    et_login_view_timeout.setError("请输入大于3的数");
                    return;
                }
                if (Integer.valueOf(success_refresh_timeout)<1){
                    et_is_success_refresh_timeout.setError("请输入>=1的数");
                    return;
                }
                if (Integer.valueOf(failure_refresh_timeout)<10){
                    et_is_failure_refresh_timeout.setError("请输入>=10的数");
                    return;
                }

                settingConfigData.saveSetting_view_timeout(Integer.valueOf(setting_view_timeout));
                settingConfigData.saveLogin_view_timeout(Integer.valueOf(login_view_timeout));
                settingConfigData.saveSuccess_refresh_timeout(Integer.valueOf(success_refresh_timeout));
                settingConfigData.saveFailure_refresh_timeout(Integer.valueOf(failure_refresh_timeout));
                Toast.makeText(context,"保存完成",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
